package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shopping")
public class CartControllerByZhao {

    @Reference(interfaceClass = ICartService.class,timeout = 30000, retries = 0, check = false)
    ICartService cartService;

    //获得购物车列表
    @GetMapping("/carts")
    public ResponseData cartList(HttpServletRequest servletRequest){
        CartListByIdRequest request = new CartListByIdRequest();
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);
        CartListByIdResponse response = cartService.getCartListById(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //添加商品到购物车
    @PostMapping("/carts")
    public ResponseData addItem2Cart(@RequestBody Map<String,Object> map){
        AddCartRequest request = new AddCartRequest();
        String userId = (String) map.get("userId");
        Long productId =((Integer)map.get("productId")).longValue();
        Integer num = (Integer) map.get("productNum");
        long l = Long.parseLong(userId);
        request.setUserId(l);
        request.setItemId(productId);
        request.setNum(num);
        AddCartResponse response = cartService.addToCart(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //更新购物车中的商品
    @PutMapping("/carts")
    public ResponseData updateCart(@RequestBody UpdateCartNumRequest request){
//        UpdateCartNumRequest request = new UpdateCartNumRequest();
//        Long userId =Long.parseLong((String)map.get("userId"));
//        Long productId =Long.parseLong((Integer)map.get("productId"));
//        String productNum = (String) map.get("productNum");
//        Integer num = Integer.valueOf(productNum);
//        String checked = (String) map.get("checked");
//        request.setUserId(userId);
//        request.setItemId(productId);
//        request.setNum(num);
//        request.setChecked(checked);
//        System.out.println(request);
//        if ("1".equals(request.getChecked())){
//            request.setChecked("true");}
//        }else {
//            request.setChecked("false");
//        }
        UpdateCartNumResponse response = cartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //全选购物车中商品
    @PutMapping("/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest request){
        CheckAllItemResponse response = cartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    //删除购物车中指定商品
    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData cartItemDel(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid){
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        DeleteCartItemResponse response = cartService.deleteCartItem(request);
        if (response.equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getMsg());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    //删除购物车中选中商品
    @DeleteMapping("/items/{id}")
    public ResponseData cartCheckedItemDel(@PathVariable("id")Long id){
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(id);
        DeleteCheckedItemResposne response = cartService.deleteCheckedItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
